package com.yincheng.oes.business.franchisee.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.yincheng.oes.business.franchisee.CommitResponse;
import com.yincheng.oes.business.franchisee.entity.BCInfo;
import com.yincheng.oes.business.franchisee.entity.FranList;
import com.yincheng.oes.business.franchisee.entity.FranStudent;
import com.yincheng.oes.business.franchisee.service.FranStudentService;

@Controller
@RequestMapping("/external/userCollection")
public class UserCollectionController {

    @Autowired
    private FranStudentService franStudentService;

    static final String failure = "FAIL";
    static final String success = "OK";

    /**
     * 收集学生信息页面
     *
     * @param view
     * @param franId
     * @return
     */
    @RequestMapping(value = "/getColletionPage.html", method = RequestMethod.GET)
    public ModelAndView draftPaper(ModelAndView view, String franId) {
        view.addObject("franId", franId);
        FranList franInfo = franStudentService.getFranInfo(franId);
        BCInfo bcInfo = franStudentService.getBCInfo();
        view.addObject("form", franInfo);
        view.addObject("bcInfo", bcInfo);
        view.setViewName("/business/franchisee/collection-phone");
        return view;
    }

    @RequestMapping(value = "/commitUserInfo.json", method = RequestMethod.POST)
    @ResponseBody
    public CommitResponse commitUserInfo(HttpServletRequest request) {
        CommitResponse response = new CommitResponse();
        String telephone = request.getParameter("telePhone");
        if (StringUtils.isEmpty(telephone)) {
            response.setCode(failure);
            response.setMessage("手机号为空");
            return response;
        }
        String code = request.getParameter("code");
        Object eCode = request.getSession().getAttribute(telephone);
        if (StringUtils.isEmpty(code)) {
            response.setCode(failure);
            response.setMessage("验证码为空");
            return response;
        }
        if (!code.equals(eCode)) {
            response.setCode(failure);
            response.setMessage("验证码不正确");
            return response;
        }
        String franId = request.getParameter("franId");
        FranStudent bean = new FranStudent();
        bean.setFranId(franId);
        bean.setPhone(telephone);
        franStudentService.commitUserInfo(bean);
        response.setCode(success);
        return response;
    }

    @RequestMapping(value = "/redirectToBroadcast", method = RequestMethod.POST)
    public String redirectToBroadcast(ModelAndView view) {
        return "redirect:http://live.bilibili.com/14665390";
    }
}
